﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using MSBTC.Model;
using MSBTC.WinFormHelpers;
using MSBTC.CrystalReport;


namespace MSBTC.ReportViewer
{
    public partial class MatEstimationRptForm : Form

    {

        PMSContext dbcontext = new PMSContext();
        FormHelpers help = new FormHelpers();
        MatEs prpt = new MatEs();

        public MatEstimationRptForm()
        {
            InitializeComponent();
        }

        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {

            if (help.PrintStat() == "nALL")
            {
                string mename = help.Pname();

                var proj = (from pj in dbcontext.Projects
                            where pj.Project_Name == mename
                            select pj).SingleOrDefault();
   
                if (proj != null)
                {
                    #region comment
                     //var mates = (from m in dbcontext.Materials
                     //            join mat in dbcontext.MaterialsInventorys on m.MaterialsInventoryid_FK equals mat.MaterialsInventoryid_PK
                     //            join mt in dbcontext.PrjMatls on mat.MaterialsInventoryid_PK equals mt.MaterialsInventoryid_FK
                     //            join p in dbcontext.Projects on mt.Projectid_FK equals p.Projectid_PK
                     //            where p.Projectid_PK == proj.Projectid_PK && mt.Projectid_FK == proj.Projectid_PK && mt.MaterialsInventoryid_FK == m.MaterialsInventoryid_FK && m.Projectid_FK == proj.Projectid_PK
                     //            select new
                     //            {
                     //                //var mates = (from mt in dbcontext.MaterialsInventorys
                     //                //             join pjmt in dbcontext.PrjMatls on mt.MaterialsInventoryid_PK equals pjmt.MaterialsInventoryid_FK
                     //                //             join p in dbcontext.Materials on pjmt.MaterialsInventoryid_FK equals p.MaterialsInventoryid_FK
                     //                //             join pj in dbcontext.Projects on p.Projectid_FK equals pj.Projectid_PK
                     //                //             where p.Projectid_FK == proj.Projectid_PK && pjmt.Projectid_FK == proj.Projectid_PK
                     //                //             select new Mat
                    //                // 
                    //var matesti = (from pj in dbcontext.Projects
                    //               join pjmt in dbcontext.PrjMatls on pj.Projectid_PK equals pjmt.Projectid_FK
                    //               join mt in dbcontext.Materials on
                    //               new { pjmt.Projectid_FK, pjmt.MaterialsInventoryid_FK } equals new { mt.Projectid_FK, mt.MaterialsInventoryid_FK }
                    //               join m in dbcontext.MaterialsInventorys on pjmt.MaterialsInventoryid_FK equals m.MaterialsInventoryid_PK
                    //               where pj.Projectid_PK == proj.Projectid_PK && pjmt.Projectid_FK == proj.Projectid_PK
                    #endregion

                    #region rpt
                    var matesti = (from mat in dbcontext.Materials
                                   join pm in dbcontext.PrjMatls on mat.MaterialsInventoryid_FK equals pm.MaterialsInventoryid_FK
                                   join m in dbcontext.MaterialsInventorys on pm.MaterialsInventoryid_FK equals m.MaterialsInventoryid_PK
                                   join p in dbcontext.Projects on mat.Projectid_FK equals p.Projectid_PK
                                   where p.Projectid_PK == proj.Projectid_PK && mat.Projectid_FK == proj.Projectid_PK && pm.Projectid_FK == proj.Projectid_PK //&& mat.MaterialsInventoryid_FK == m.MaterialsInventoryid_PK// && pm.Projectid_FK == p.Projectid_PK && pjmt.MaterialsInventoryid_FK == mat.MaterialsInventoryid_FK
                                   select new
                                   {
                                       m.Material_Name,
                                       mat.Quantity,
                                       mat.Unit,
                                       mat.Direct_Cost,
                                       mat.Markupin_OCM,
                                       mat.Markupin_Profit,
                                       mat.TotMarkup_pct,
                                       mat.TotMarkup_Value,
                                       mat.VAT,
                                       mat.TotIndirectcost,
                                       mat.Tot_Cost,
                                       mat.Unit_Cost
                                   }).ToList();

                    if (matesti.Count != 0)
                    {
                        TextObject txtobj;
                        txtobj = (TextObject)prpt.ReportDefinition.ReportObjects["txtPname"];
                        txtobj.Text = proj.Project_Name;

                        MatEsti rpt = new MatEsti();
                        
                        rpt.SetDataSource(matesti);
                        //rpt.Refresh();

                        crysMatEsti.ReportSource = prpt;
                        crysMatEsti.RefreshReport();
                    }
                }
#endregion
            }

        }

    }

}
    

